package com.oasystem.dao.jdbc.system;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;

import com.oasystem.dao.jdbc.BaseDAO;
import com.oasystem.domain.fund.FundClassification;
import com.oasystem.utils.PrimaryKeyGenerator;

public class FundClassifyDAO extends BaseDAO {
	public boolean isClassifyExist(String classifyName) {
		String sql = "SELECT COUNT(*) FROM fundClassify WHERE classify=?";
		return getJdbcTemplate().queryForInt(sql,
				new Object[] { classifyName }, new int[] { Types.VARCHAR }) > 0;
	}

	public void save(String classifyName) {
		String sql = "INSERT INTO fundClassify values(?,?)";
		getJdbcTemplate().update(sql,
				new Object[] { PrimaryKeyGenerator.getUUID(), classifyName },
				new int[] { Types.VARCHAR, Types.VARCHAR });
	}

	public List<FundClassification> fundClassifyList() {
		String sql = "SELECT id,name AS name FROM fundClassification";
		return getJdbcTemplate().query(sql,
				new RowMapper<FundClassification>() {

					@Override
					public FundClassification mapRow(ResultSet rs, int index)
							throws SQLException {
						FundClassification fundClassification = new FundClassification();
						fundClassification.setId(rs.getString(1));
						fundClassification.setName(rs.getString(2));
						return fundClassification;
					}
				});
	}

	public void deleteClassify(String classifyId) {
		String sql = "DELETE FROM fundClassify WHERE id=?";
		getJdbcTemplate().update(sql, new Object[] { classifyId },
				new int[] { Types.VARCHAR });
	}

	public void updateClassify(String classifyId, String newClassify) {
		String sql = "UPDATE fundClassify SET classify = ? WHERE id=?";
		getJdbcTemplate().update(sql, new Object[] { newClassify, classifyId },
				new int[] { Types.VARCHAR, Types.VARCHAR });
	}
}
